import _ from 'lodash';
import moment from 'moment';
import { isDark } from '#/utils/utils';

export const TABSELECT = [
  {
    name: 'α 版',
    code: 'firstNew',
  },
  {
    name: 'β 版',
    code: 'secondNew',
  },
];

export const TYPECOLOR = {
  0: {
    name: '电价预测1',
    color: '#F58E2A',
  },
  1: {
    name: '日前电价',
    color: '#27AE60',
  },
  2: {
    name: '电价预测2',
    color: '#56CCF2',
  },
};
export const typeName = {
  电价预测1: TYPECOLOR[0].color,
  电价预测2: TYPECOLOR[2].color,
  日前电价: TYPECOLOR[1].color,
};

//96点循环
const timeData = () => {
  let dataTimes = [];
  let time = moment().startOf('date');
  _.forEach(new Array(96), () => {
    time.add(15, 'minutes');
    const tK = time.format('HH:mm') === '00:00' ? '24:00' : time.format('HH:mm');
    dataTimes.push(tK);
  });
  return dataTimes;
};
let dataTime = timeData();

export const curveOption = function (
  { list, forecastUpdateTime, rqdjUpdateTime }, //数据
  ctooltip,
  theme,
  start = 0,
) {
  // renovate.isRenovate = false;
  const lists = JSON.parse(JSON.stringify(list));
  // let lists = changeLegend(listArr);
  // yAxisData = [];
  let xAxisData = [];
  let series = []; //图表显示数据的集合
  let legendData = []; //底部可点击名字列表
  let xData = [];
  // let _legendData=[];
  //   let boundaryGap = false;
  list.map((i) => {
    xData.push(...Object.keys(i?.list));
  });
  Array.from(new Set(xData)).forEach((i) => {
    dataTime.forEach((m) => {
      if (m === '24:00') {
        const dataTime = `${i} ${m}`;
        xAxisData.push(dataTime.replace(/[-|.|:]/g, ''));
      } else {
        xAxisData.push(`${moment(`${i} ${m}`).format('YYYYMMDD HHmm')}`);
      }
    });
  });
  let tooltip = {
    trigger: 'axis',
    extraCssText: 'box-shadow: 0 0 10px rgba(0, 0, 0, 0.1)',
    backgroundColor: 'rgba(255, 255, 255, 1)',
    borderColor: isDark() ? '#444444' : '#1A253C4B',
    borderWidth: 0,
    textStyle: { color: '#888E95', borderWidth: 0, borderColor: '#1A253C4B', padding: 12 },
  };
  tooltip.axisPointer = {
    type: 'cross',
    label: {
      backgroundColor: isDark() ? '#444444' : '#F5F8FA',
      color: isDark() ? 'white' : '#888E95',
      shadowBlur: 0,
    },
  };
  tooltip.formatter = ctooltip;
  let listData = lists.filter((item) => {
    return item;
  });
  listData.forEach((item, index) => {
    let name = item.name || TYPECOLOR[item?.curveType].name;
    let array = [];
    let objlegendData = {};
    // objlegendData['icon'] =  'circle'
    objlegendData['name'] = name;
    objlegendData['itemStyle'] = {
      opacity: 1,
      borderWidth: 1,
      borderColor: TYPECOLOR[item?.curveType]?.color,
      color: '#1F1F1F',
    };
    legendData.push(objlegendData);
    var serieObj = {
      id: index,
      name: name,
      type: 'line',
    };
    const listArr = Object.values(item?.list);
    listArr.map((i) => {
      array.push(
        ...(96 - i?.length > 0 ? i.concat(Array.from({ length: 96 - i?.length }, (_) => '-')) : i),
      );
    });
    let symbol = 'circle';
    serieObj.color = TYPECOLOR[item?.curveType]?.color;
    serieObj.data = array;
    // serieObj.curveType = item.serieObj;
    serieObj.symbol = symbol;
    serieObj.showSymbol = false;
    // serieObj.symbolSize=[100, 1],
    // serieObj.step = 'middle';
    serieObj.smooth = true;
    (serieObj.lineStyle = {
      width: 1,
    }),
      series.push(serieObj);
  });
  let option = {
    grid: { height: '80%', containLabel: true, left: 0, right: 20, top: '10%' },
    tooltip: tooltip,
    dataZoom: [
      {
        type: 'slider',
        height: 0,
        bottom: '20',
        left: '20',
        right: '20',
        show: false,
        minSpan: 1,
        startValue: start,
        throttle: 500,
        dataBackground: {
          areaStyle: {
            color: '#757575',
            opacity: 0.5,
          },
        },
      },
      {
        type: 'inside',
        show: false,
        minSpan: 1,
        throttle: 500,
      },
    ],
    legend: {
      data: legendData,
      bottom: '0%',
      type: 'scroll',
      borderWidth: '0',
      textStyle: {
        color: isDark() ? 'white' : '',
        rich: {
          update_time: {
            color: '#888E95',
          },
        },
      },
      // 图表的图例需要添加更新时间
      // 所以要自定义formatter去区分图例线名称和更新时间的样式
      // formatter: function (name) {
      //   // 电价预测更新时间
      //   if (name == '电价预测1') {
      //     let f = forecastUpdateTime || '-'
      //     return (name + '{update_time|(' + f + ')}')
      //   }
      //   // 日前电价更新时间
      //   if (name == '日前电价') {
      //     let r = rqdjUpdateTime || '-'
      //     return (name + '{update_time|(' + r + ')}')
      //   }

      //   if(name== '电价预测2'){
      //     let f = forecastUpdateTime || '-'
      //     return (name + '{update_time|(' + f + ')}')
      //   }
      //   return name

      // },
      padding: [10, 80, 0, 45],
      itemGap: 10,
      itemHeight: 13,
      itemWidth: 20,
      inactiveColor: '#545454',
      inactiveBorderColor: '#545454',
      inactiveBorderWidth: 'inherit',
    },
    xAxis: {
      type: 'category',
      data: xAxisData,
      // boundaryGap: boundaryGap,
      // boundaryGap: ['2%', '2%'],
      axisTick: { alignWithLabel: true },
      nameTextStyle: { color: '#888E95' },
      axisLabel: { color: '#888E95' },
      axisLine: { lineStyle: { color: '#464646' } },
      axisPointer: {
        label: {
          backgroundColor: isDark() ? '#444444' : '#F5F8FA',
          color: isDark() ? 'white' : '#888E95',
          shadowBlur: 0,
        },
      },
    },
    yAxis: {
      type: 'value',
      scale: true,
      splitNumber: 4,
      axisLine: { show: false },
      axisTick: { show: false },
      nameTextStyle: { color: '#888E95' },
      axisLabel: { color: '#888E95' },
      splitLine: { lineStyle: { color: isDark() ? '#363636' : '#464646' } },
      axisPointer: {
        label: {
          backgroundColor: isDark() ? '#444444' : '#F5F8FA',
          color: isDark() ? 'white' : '#888E95',
          shadowBlur: 0,
        },
      },
    },
    series: series,
  };
  // series.forEach(Jitem => {
  //   if (Jitem.id === 1) {
  //     Jitem.data.forEach((item) => {
  //       yAxisData.push(item.value);
  //     });
  //   }
  // });
  return option;
};
export const tformatCopper = function (value) {
  if (value === undefined || value === null) {
    return null;
  }
  return Number(parseFloat(value).toFixed(3));
};

// const xAxisFormatter = function (params) {
//   let item = params.seriesData[0];
//   return (item && item.name) || '';
// };
export const formatCopper = function (value) {
  if (value === undefined || value === null) {
    return '-';
  }
  if (isNaN(value)) {
    return '-';
  }

  return Number(parseFloat(value).toFixed(3));
};

export const formatPriceRoundUp = (value, precision = 3) => {
  if (value === null || value === undefined) {
    return value;
  }
  if (isNaN(value)) {
    return '-';
  }
  let num = Math.round(value * 10 ** precision) / 10 ** precision;
  let numData = num.toFixed(precision);

  return numData;
};
